﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BusinessLayer.Model.Services.Abstract;
using BusinessLayer;

namespace DataAccess.Services.Abstract
{
    /// <summary>
    /// Handles all exceptions thrown.
    /// Autor original: Rafael Belliard
    /// </summary>
    public class ExceptionReportService : IExceptionReportService
    {

        /// <summary>
        /// Logs every exception thrown
        /// </summary>
        public void LogException(Exception exc, string source, string url)
        {
            ApplicationService appService = new ApplicationService();

            string logPath = appService.LogsFolder;
            FileInfo fileInfo = new FileInfo(logPath + Constants.ErrorLogFile);

            StreamWriter swError = new StreamWriter(fileInfo.FullName, true);
            swError.WriteLine("********** {0} **********", DateTime.Now);

            if (exc.InnerException != null)
            {
                swError.WriteLine("Inner Exception Type: " + exc.InnerException.GetType());
                swError.WriteLine("Inner Exception: {0}", exc.InnerException.Message);
                swError.WriteLine("Inner Source: {0}", exc.InnerException.Source);

                if (exc.InnerException.StackTrace != null)
                {
                    swError.WriteLine("Inner Stack Trace: ");
                    swError.WriteLine(exc.InnerException.StackTrace);
                }
            }

            swError.WriteLine("Exception Type: {0}", exc.GetType());
            swError.WriteLine("Request URL: {0}", url);
            swError.WriteLine("Exception: {0}", exc.Message);
            swError.WriteLine("Source: {0}", source);

            if (exc.StackTrace != null)
            {
                swError.WriteLine("Stack Trace: ");
                swError.WriteLine(exc.StackTrace);
                swError.WriteLine();
            }

            swError.Close();

        }

        /// <summary>
        /// Gets the exception string.
        /// </summary>
        public void LogExceptionInHtml(Exception exc, string url)
        {

            ApplicationService appService = new ApplicationService();

            string logPath = appService.LogsFolder;
            //string logPath = ConfigurationManager.AppSettings["BoletinINTEC.LOG_PATH"];
            FileInfo fileInfo = new FileInfo(logPath + Constants.ErrorHtmlLogFile);

            StreamWriter sw = new StreamWriter(fileInfo.FullName, true);

            StringBuilder sbError = new StringBuilder();

            sbError.AppendLine("<h3>Exception thrown: </h3>");
            sbError.AppendLine(string.Format("<h4><span style=\"color:Red\">{0}</span></h4>", exc.Message));
            sbError.AppendLine("<hr />");
            sbError.AppendLine("<h3>Exception details:</h3>");

            sbError.AppendLine("<div style=\"font: 11px verdana, arial\">");
            sbError.AppendLine(string.Format("********** <strong>{0}</strong> **********", DateTime.Now));
            sbError.AppendLine(string.Format("<br /><strong>Exception Type:</strong> {0}", exc.GetType()));
            sbError.AppendLine(string.Format("<br /><strong>Request URL:</strong> <a href=\"{0}\">{0}</a>", url));
            sbError.AppendLine(string.Format("<br /><strong>Exception:</strong> {0}", exc.Message));
            sbError.AppendLine(string.Format("<br /><strong>Source:</strong> {0}", exc.Source));

            if (exc.StackTrace != null)
            {
                sbError.AppendLine("<br /><strong>Stack Trace:</strong><br /><ul><li>");
                sbError.AppendLine(exc.StackTrace.Replace("   at ", "</li><li>at "));
                sbError.AppendLine("</ul>");
                sbError.AppendLine("</div>");
            }


            if (exc.InnerException != null)
            {
                sbError.AppendLine("<hr />");
                sbError.AppendLine("<h3>Inner Exception details:</h3>");
                sbError.Append("<div style=\"font: 11px verdana, arial\">");
                sbError.AppendLine(string.Format("<strong>Inner Exception Type:</strong> {0}", exc.InnerException.GetType()));
                sbError.AppendLine(string.Format("<br /><strong>Inner Exception:</strong> {0}", exc.InnerException.Message));
                sbError.AppendLine(string.Format("<br /><strong>Inner Source:</strong> {0}", exc.InnerException.Source));

                if (exc.InnerException.StackTrace != null)
                {
                    sbError.AppendLine("<br /><strong>Inner Stack Trace:</strong><br /><ul><li>");
                    sbError.AppendLine(exc.InnerException.StackTrace.Replace("   at ", "</li><li>at "));
                    sbError.AppendLine("</ul>");
                    sbError.AppendLine();
                }
            }

            sbError.AppendLine("\n<hr />\n");

            sw.Write(sbError);
            sw.Close();
        }
    }
}
